import { useCallback, useState } from 'react';
import type { MessageItem } from '@/pages/Message/data';
import { queryNotices } from '@/services/ant-design-pro/notification';


export default function noticeState() {

  const [ notices, setNotices ] = useState<MessageItem[]>([]);
  const [ loading, setLoading ] = useState(false);
  const [ unreadCount, setUnreadCount ] = useState(0);

  const fetchNotices = useCallback(async () => {

    setLoading(true);
    const response = await queryNotices();

    setNotices(response.data.list);

    const count: number = response.data.list.filter(item => !item.received).length;

    setUnreadCount(count);

    setLoading(false);

  }, []);


  return {
    notices,
    loading,
    unreadCount,
    fetchNotices,
  };
};
